package fr.cephb.joperon.core.db;



import java.util.HashSet;
import java.util.Set;

import com.sleepycat.db.DatabaseException;


import fr.cephb.joperon.core.Operon;

import fr.cephb.joperon.core.db.base.BaseMPanelDB;
import fr.cephb.joperon.core.entities.MPanel;
import fr.cephb.joperon.core.iterator.FilterIterator;
import fr.cephb.util.Pair;






/**
 * SnpMapDB
 * @author lindenb
 *
 */
public class MPanelDB
	extends BaseMPanelDB
	{
	public MPanelDB(Operon operon)
		{
		super(operon);
		}
	
	public Integer getMaxUid() throws DatabaseException
		{
		Integer m= getKeyMax();
		return (m==null?1:m+1);
		}
	
	public MPanel findMPanelBySyn(String syn) throws DatabaseException
		{
		return new FilterIterator<MPanel,String>(listValues(),syn)
			{
			@Override
			public boolean accept(MPanel object) {
				return object.getSyn()!=null &&
						object.getSyn().equalsIgnoreCase(getUserValue());
				}
			}.nextOrNull();
		}
	
	/** returns the mpanels for CEU/CHB/JPT/YRI */
	public Set<MPanel> getTheFourHapmapFamilies() throws DatabaseException
		{
		Set<Integer> set= new HashSet<Integer>(4);
		set.add(MPanel.CEU);
		set.add(MPanel.CHB);
		set.add(MPanel.JPT);
		set.add(MPanel.YRI);
		return new HashSet<MPanel>(get(set).values());
		}
	
	public static void main(String[] args) {
		Operon op=null;
		MPanelDB db=null;
		try {
			op= Operon.newInstance();
			db= new MPanelDB(op);
			db.open();
			KeyValueIterator iter=db.listKeyValues();
			while(iter.hasNext())
				{
				Pair<?, ?> o=iter.next();
				System.err.println(o);
				}
			iter.close();
		} catch (Exception e) {
			e.printStackTrace();
			}
		finally
			{
			if(db!=null) db.safeClose();
			if(op!=null) op.safeClose();
			}
		}
	
	}


